package com.huatai.vba.controller;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import com.google.common.collect.Lists;
import com.huatai.vba.common.utils.ToStringUtils;
import com.huatai.vba.service.VbaExtendsService;
import com.huatai.vba.vo.CompanyData;

@RestController
@RequestMapping(value = "compare")
public class CompareController {

	private static final Logger logger = LoggerFactory.getLogger(ActionController.class);

	@Autowired
	private VbaExtendsService vbaExtendsService;

	@RequestMapping(value = "", method = RequestMethod.GET)
	public ModelAndView compare() {
		ModelAndView mv = new ModelAndView("compare");

		return mv;
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ResponseBody
	public List<CompanyData> list() {
		List<CompanyData> list = Lists.newArrayList();
		String yyyyMMdd = new SimpleDateFormat("yyyyMMdd").format(new Date());
		String sql = "select * from comparedata where d_date ='" + yyyyMMdd + "' and c_fundname is not null";
		try {
			List<Map<String, Object>> resultList = vbaExtendsService.querySql(sql);
			for (Map<String, Object> map : resultList) {
				CompanyData data = new CompanyData();
				data.setD_date((String) map.get("d_date".toUpperCase()));
				data.setD_fundcode((String) map.get("c_fundcode".toUpperCase()));
				data.setD_fundname((String) map.get("c_fundname".toUpperCase()));
				BigDecimal ta_f_lastasset = new BigDecimal(map.get("ta_f_lastasset".toUpperCase()) + "");
				BigDecimal gz_f_lastasset = new BigDecimal(map.get("gz_f_lastasset".toUpperCase()) + "");
				BigDecimal ta_f_lastshares = new BigDecimal(map.get("ta_f_lastshares".toUpperCase()) + "");
				BigDecimal gz_f_lastshares = new BigDecimal(map.get("gz_f_lastshares".toUpperCase()) + "");
				BigDecimal ta_f_netvalue = new BigDecimal(map.get("ta_f_netvalue".toUpperCase()) + "");
				BigDecimal gz_f_netvalue = new BigDecimal(map.get("gz_f_netvalue".toUpperCase()) + "");
				BigDecimal ta_f_totalnetvalue = new BigDecimal(map.get("ta_f_totalnetvalue".toUpperCase()) + "");
				BigDecimal gz_f_totalnetvalue = new BigDecimal(map.get("gz_f_totalnetvalue".toUpperCase()) + "");
				if (ta_f_lastasset.subtract(gz_f_lastasset).doubleValue() == 0) {
					data.setTa_f_lastasset(ToStringUtils.decimalToString(ta_f_lastasset, 2));
					data.setGz_f_lastasset(ToStringUtils.decimalToString(gz_f_lastasset, 2));
				} else {
					data.setTa_f_lastasset(addRedDiv(ToStringUtils.decimalToString(ta_f_lastasset, 2)));
					data.setGz_f_lastasset(addRedDiv(ToStringUtils.decimalToString(gz_f_lastasset, 2)));
				}
				if (ta_f_lastshares.subtract(gz_f_lastshares).doubleValue() == 0) {
					data.setTa_f_lastshares(ToStringUtils.decimalToString(ta_f_lastshares, 2));
					data.setGz_f_lastshares(ToStringUtils.decimalToString(gz_f_lastshares, 2));
				} else {
					data.setTa_f_lastshares(addRedDiv(ToStringUtils.decimalToString(ta_f_lastshares, 2)));
					data.setGz_f_lastshares(addRedDiv(ToStringUtils.decimalToString(gz_f_lastshares, 2)));
				}
				if (ta_f_netvalue.subtract(gz_f_netvalue).doubleValue() == 0) {
					data.setTa_f_netvalue(ToStringUtils.decimalToString(ta_f_netvalue, 4));
					data.setGz_f_netvalue(ToStringUtils.decimalToString(gz_f_netvalue, 4));
				} else {
					data.setTa_f_netvalue(addRedDiv(ToStringUtils.decimalToString(ta_f_netvalue, 4)));
					data.setGz_f_netvalue(addRedDiv(ToStringUtils.decimalToString(gz_f_netvalue, 4)));
				}
				if (ta_f_totalnetvalue.subtract(gz_f_totalnetvalue).doubleValue() == 0) {
					data.setTa_f_totalnetvalue(ToStringUtils.decimalToString(ta_f_totalnetvalue, 4));
					data.setGz_f_totalnetvalue(ToStringUtils.decimalToString(gz_f_totalnetvalue, 4));
				} else {
					data.setTa_f_totalnetvalue(addRedDiv(ToStringUtils.decimalToString(ta_f_totalnetvalue, 4)));
					data.setGz_f_totalnetvalue(addRedDiv(ToStringUtils.decimalToString(gz_f_totalnetvalue, 4)));
				}
				list.add(data);
			}
		} catch (Exception e) {
			logger.error("系统异常", e);
		}
		return list;
	}

	private String addRedDiv(String field) {
		return "<div style='color:red;'><strong>" + field + "</strong></div>";
	}

}
